package com.mbyte.easy.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mbyte.easy.admin.entity.CheckPartner;
import com.mbyte.easy.admin.entity.Relation;
import com.mbyte.easy.admin.mapper.CheckPartnerMapper;
import com.mbyte.easy.admin.mapper.RelationMapper;
import com.mbyte.easy.admin.service.ICheckPartnerService;
import com.mbyte.easy.admin.service.IDraftService;
import com.mbyte.easy.common.constant.SysRole;
import com.mbyte.easy.security.entity.SysUser;
import com.mbyte.easy.security.mapper.SysUserMapper;
import com.mbyte.easy.security.service.IUserService;
import com.mbyte.easy.util.Utility;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 互查伙伴关系表 服务实现类
 * </p>
 *
 * @author lp
 * @since 2019-12-07
 */
@Slf4j
@Service
public class CheckPartnerServiceImpl extends ServiceImpl<CheckPartnerMapper, CheckPartner> implements ICheckPartnerService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private CheckPartnerMapper checkPartnerMapper;
    @Autowired
    private RelationMapper relationMapper;
    @Autowired
    private IUserService userService;
    @Autowired
    private IDraftService draftService;

    /**
     * @Description 版块负责人查看自己的组员列表
     * @Author myr
     * @param pageNo
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public IPage index(Integer pageNo, Integer pageSize, String name, Model model) {
        String loginUserName = Utility.getCurrentUsername();
        SysUser loginUser = sysUserMapper.selectByUsername(loginUserName);
        Long modulLeaderId = loginUser.getId();
        QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
        if(draftService.selectRoleId() == SysRole.Block_Leader){
            queryWrapper.eq("parent_id", modulLeaderId).eq("role_id", SysRole.Writer);
        }else if(draftService.selectRoleId() == SysRole.BLOCK_SEVEN){
            queryWrapper.eq("parent_id", modulLeaderId).eq("role_id", SysRole.PARTTIME_SEVEN);
        }else if(draftService.selectRoleId() == SysRole.BLOCK_SIX){
            queryWrapper.eq("parent_id", modulLeaderId).eq("role_id", SysRole.PARTTIME_SIX);
        }
        if (name != null && !"".equals(name.trim())){
            queryWrapper.eq("user_id",sysUserMapper.selectByUsername(name).getId());
        }
        List<Relation> relations = relationMapper.selectList(queryWrapper);
        List<Long> writerIds = relations.stream().map(Relation::getUserId).collect(Collectors.toList());
        // 空集合
        if(CollectionUtils.isEmpty(writerIds)){
            writerIds.add(-1L);
        }
        IPage<SysUser> sysUserIPage = sysUserMapper.selectByUserIdForPage(new Page<SysUser>(pageNo,pageSize), writerIds);
        return sysUserIPage;
    }

    /**
     * @Description 获取同一个组的所有写手,除了自己
     * @Author myr
     * @param id 要添加互查伙伴的写手id
     * @return
     */
    @Override
    public List<SysUser> addBefore(Long id) {
        String loginUserName = Utility.getCurrentUsername();
        SysUser loginUser = sysUserMapper.selectByUsername(loginUserName);
        Long modulLeaderId = loginUser.getId();
        QueryWrapper<CheckPartner> queryWrapper = new QueryWrapper<>();
        long roleId = draftService.selectRoleId();
        log.info("+++++===+++++++++++++++++++++++++++++++++++++++++++++++++");
        queryWrapper.eq("module_leader", modulLeaderId).ne("user_id", id);
        List<CheckPartner> partners = checkPartnerMapper.selectList(queryWrapper);
        //去掉别人的互查伙伴
        List<CheckPartner> checkPartnerList  =new ArrayList<>();
        QueryWrapper<CheckPartner> qw = new QueryWrapper<>();
        qw.eq("module_leader",modulLeaderId).isNotNull("partner_id");
        List<CheckPartner> list = checkPartnerMapper.selectList(qw);
        List<Long> list2 = new ArrayList<Long>();
        for(CheckPartner listOne:list){
            list2.add(listOne.getPartnerId());
        }
        for(CheckPartner partner:partners){
            if(!list2.contains(partner.getUserId())) {
                checkPartnerList.add(partner);
            }
        }
        List<Long> writerIds = checkPartnerList.stream().map(CheckPartner::getUserId).collect(Collectors.toList());
        log.info("writerIds===="+writerIds);
        if (writerIds.size() != 0){
            IPage<SysUser> sysUserIPage = sysUserMapper.selectByUserIdForPage(new Page<SysUser>(1,20), writerIds);
            return sysUserIPage.getRecords();
        }else{
            IPage<SysUser> sysUserIPage = (new Page<SysUser>(1,20));
            return sysUserIPage.getRecords();
        }
    }

    /**
     * @Description 获取当前登录用户
     * @Author myr
     * @return
     */
    @Override
    public SysUser getCurrentUser(){
        String loginUserName = Utility.getCurrentUsername();
        SysUser loginUser = sysUserMapper.selectByUsername(loginUserName);
        return loginUser;
    }

    /**
     * @Description 获取当前写手的互查伙伴信息,用于回显
     * @Author myr
     * @param userId 要编辑互查伙伴的写手id
     * @return
     */
    @Override
    public CheckPartner getPartner(Long userId){
        QueryWrapper<CheckPartner> eq = new QueryWrapper<CheckPartner>();
                eq.eq("user_id", userId).eq("module_leader", getCurrentUser().getId());
        List<CheckPartner> checkPartners = checkPartnerMapper.selectList(eq);
        if(checkPartners.isEmpty()){
            return null;
        }else {
            return checkPartners.get(0);
        }
    }
}
